package com.aikon.wht.queue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author haitao.wang
 */
public class DelayModel<T> implements Delayed {

    public static final long ORIGIN = System.currentTimeMillis();

    T t;

    long time;

    public DelayModel(T t) {
        this.time = System.currentTimeMillis() - ORIGIN;
        this.t = t;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - (System.currentTimeMillis() - ORIGIN);
    }

    @Override
    public int compareTo(Delayed o) {
        long d = this.getDelay(TimeUnit.SECONDS) - o.getDelay(TimeUnit.SECONDS);
        return (d == 0) ? 0 : (d > 0 ? 1 : -1);
    }
}
